﻿/***********************************************************
Copyright © 2015 Howyoung.
************************************************************
You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty.
************************************************************
您可以对所收受的本程序源代码，无论以何种媒介，复制与发布其完
整的复制物，然而您必须以显著及适当的方式在每一份复制物上发布
适当的著作权标示及无担保声明。
***********************************************************/
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace UpdateProgram
{
    public partial class Setup : Form
    {
        public Setup()
        {
            InitializeComponent();
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            try
            {
                string uninstallString = null;
                RegistryKey uninstallKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall", true);
                foreach (string keyName in uninstallKey.GetSubKeyNames())
                {
                    RegistryKey appKey = uninstallKey.OpenSubKey(keyName);
                    if (Convert.ToString(appKey.GetValue("DisplayName")) == "Minecraft存档备份")
                    {
                        uninstallString = Convert.ToString(appKey.GetValue("UninstallString"));
                    }
                }
                if (uninstallString == null)
                {
                    MessageBox.Show("原程序已被卸载，无法回到上一步。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe", "/c " + uninstallString);
                    processStartInfo.UseShellExecute = false;
                    processStartInfo.CreateNoWindow = true;
                    Process.Start(processStartInfo);
                    Uninstall Uninstall = new Uninstall();
                    Uninstall.Show();
                    Close();
                }
            }
            catch (Exception)
            { }
        }

        private void Setup_Load(object sender, EventArgs e)
        {
            try
            {
                bool programFound = false;
                RegistryKey uninstallKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall", true);
                foreach (string keyName in uninstallKey.GetSubKeyNames())
                {
                    RegistryKey appKey = uninstallKey.OpenSubKey(keyName);
                    if (Convert.ToString(appKey.GetValue("DisplayName")) == "Minecraft存档备份")
                    {
                        programFound = true;
                    }
                }
                backButton.Enabled = programFound;
                Process.Start(AppDomain.CurrentDomain.BaseDirectory + "setup.exe");
            }
            catch (Exception ex)
            {
                MessageBox.Show(":(" + Environment.NewLine + "很抱歉，我们遇到了麻烦。" + Environment.NewLine + Environment.NewLine +
                "这是可能有用的信息：" + Environment.NewLine + "无法启动安装程序，" + ex.Message + Environment.NewLine + Environment.NewLine +
                "您可以试试：" + Environment.NewLine + "请再试一次。", "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            try
            {
                bool programFound = false;
                RegistryKey uninstallKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall", true);
                foreach (string keyName in uninstallKey.GetSubKeyNames())
                {
                    RegistryKey appKey = uninstallKey.OpenSubKey(keyName);
                    if (Convert.ToString(appKey.GetValue("DisplayName")) == "Minecraft 存档备份")
                    {
                        programFound = true;
                    }
                }
                if (programFound)
                {
                    Application.ExitThread();
                }
            }
            catch (Exception)
            { }
        }

        private void finishButton_Click(object sender, EventArgs e)
        {
            bool programFound = false;
            RegistryKey uninstallKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall", true);
            foreach (string keyName in uninstallKey.GetSubKeyNames())
            {
                RegistryKey appKey = uninstallKey.OpenSubKey(keyName);
                if (Convert.ToString(appKey.GetValue("DisplayName")) == "Minecraft 存档备份")
                {
                    programFound = true;
                }
            }
            if (programFound)
            {
                Application.ExitThread();
            }
            else
            {
                if (MessageBox.Show("新程序似乎没有被安装，您确定要继续吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    Application.ExitThread();
            }
        }
    }
}
